import {
  addOrg, 
  removeOrg, 
  resetPassword, 
  updateState, 
  transferUserOrg, 
  queryOrgList, 
  queryTreeOrgList, 
  updateOrg, 
  queryTitle, 
  queryRole, 
  queryPassWord, 
  queryLevel,
  importData,
  weakpwdImport,
  weakpwdDownloadt,
  queryRoleByCode,
  checkPhoneUnique,
  checkIdnumberUnique,
  checkEmailUnique,
  } from '@/services/system/sysUser';

export default {
  namespace: 'usermanager',

  state: {
    // 树状列表
    treeList: [],

    //选中列表数据
    data: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },

    //查询条件
    queryForm: {},

  },
  // effects： 处理所有的异步逻辑，将返回结果以Action的形式交给reducer处理；
  effects: {
    * getTree({payload}, {call, put}) {
      const response = yield call(queryTreeOrgList, payload);
      yield put({
        type: 'saveTree',
        payload: response,
      });
    },
    * getList({payload, callback}, {call, put}) {
      const response = yield call(queryOrgList, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
      if (callback) callback(response);
    },
    * add({payload, callback}, {call, put}) {
      const response = yield call(addOrg, payload);

      yield put({
        type: 'saveAdd',
        payload: response,
      });
      if (callback) callback(response);
    },
    * remove({payload, callback}, {call, put}) {
      const response = yield call(removeOrg, payload);
      yield put({
        type: 'saveRemove',
        payload: response,
      });
      if (callback) callback(response);
    },
    * resetPass({payload, callback}, {call, put}) {
      const response = yield call(resetPassword, payload);
      yield put({
        type: 'saveReset',
        payload: response,
      });
      if (callback) callback(response);
    },    
    * updateS({payload, callback}, {call, put}) {
      const response = yield call(updateState, payload);
      yield put({
        type: 'saveUpdateS',
        payload: response,
      });
      if (callback) callback(response);
    },  
    * transferUserO({payload, callback}, {call, put}) {
      const response = yield call(transferUserOrg, payload);
      yield put({
        type: 'saveTransfer',
        payload: response,
      });
      if (callback) callback(response);
    },
    * update({payload, callback}, {call, put}) {
      const response = yield call(updateOrg, payload);
      yield put({
        type: 'saveUpdate',
        payload: response,
      });
      if (callback) callback(response);
    },
    // * title({payload, callback}, {call, put}) {
    //   const response = yield call(queryTitle, payload);
    //   yield put({
    //     type: 'save',
    //     payload: response,
    //   });
    //   if (callback) callback(response);
    // },
    * role({payload, callback}, {call, put}) {
      const response = yield call(queryRole, payload);
      yield put({
        type: 'saveRole',
        payload: response,
      });
      if (callback) callback(response);
    },
    * passWord({payload, callback}, {call, put}) {
      const response = yield call(queryPassWord, payload);
      yield put({
        type: 'savePsw',
        payload: response,
      });
      if (callback) callback(response);
    },
    * level({payload, callback}, {call, put}) {
      const response = yield call(queryLevel, payload);
      yield put({
        type: 'saveLevel',
        payload: response,
      });
      if (callback) callback(response);
    },
    * importData({payload, callback}, {call, put}) {
      const response = yield call(importData, payload);
      yield put({
        type: 'saveimportData',
        payload: response,
      });
      if (callback) callback(response);
    },
    * weakpwdImport({payload, callback}, {call, put}) {
      const response = yield call(weakpwdImport, payload);
      yield put({
        type: 'saveweakpwdImport',
        payload: response,
      });
      if (callback) callback(response);
    },
    * weakpwdDownloadt({payload, callback}, {call, put}) {
      const response = yield call(weakpwdDownloadt, payload);
      yield put({
        type: 'saveweakpwdDownloadt',
        payload: response,
      });
      if (callback) callback(response);
    },
    * roleByCode({payload, callback}, {call, put}) {
      const response = yield call(queryRoleByCode, payload);
      yield put({
        type: 'saveroleByCode',
        payload: response,
      });
      if (callback) callback(response);
    },
    * checkPhone({payload, callback}, {call, put}) {
      const response = yield call(checkPhoneUnique, payload);
      yield put({
        type: 'savercheckPhone',
        payload: response,
      });
      if (callback) callback(response);
    },
    * checkIdnumber({payload, callback}, {call, put}) {
      const response = yield call(checkIdnumberUnique, payload);
      yield put({
        type: 'savercheckIdnumber',
        payload: response,
      });
      if (callback) callback(response);
    },
    * checkEmail({payload, callback}, {call, put}) {
      const response = yield call(checkEmailUnique, payload);
      yield put({
        type: 'savercheckEmail',
        payload: response,
      });
      if (callback) callback(response);
    },
  },
  // reducers：处理所有的同步逻辑，将数据返回给页面；
  reducers: {
    saveTree(state, action) {
      return {
        ...state,
        treeList: action.payload.data,
      };
    },
    saveList(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
  },
};
